package com.study.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @author study
 * @version 1.0
 * @date 2020/8/18 22:38
 */
@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
@Configuration
public class MyThreadConfig {
    @Bean
    public ExecutorService executor(ThreadPoolConfigProperties pool) {
        //guava定义线程名字
        ThreadFactory nameThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("my-queue-thread-%d").build();

        return new ThreadPoolExecutor(pool.getCoreSize(),
                pool.getMaxSize(),
                pool.getKeepAliveTime(),
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(10000),
                nameThreadFactory, new ThreadPoolExecutor.AbortPolicy());

    }
}
